package com.alibaba.dubbo.demo.provider.dao.impl;

import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;

import com.alibaba.dubbo.demo.SysUserInfoInterface;
import com.alibaba.dubbo.demo.bean.PageBean;
import com.alibaba.dubbo.demo.bean.SysUserinfo;

public class SysUserInfoDaoImpl extends BaseDAO implements SysUserInfoInterface {

	Logger logger = Logger.getLogger(SysUserInfoDaoImpl.class);
	@Override
	public PageBean queryAllUserInfo(PageBean pageBean,int upperid,String usercode,String username) {
		// TODO Auto-generated method stub
		String msql = "from SysUserinfo";
		String where = "";
		if(upperid!=0)
			where = " deptid =" + upperid;
		if(StringUtils.isNotEmpty(usercode))
		{
			if(StringUtils.isNotEmpty(where))
				where += " and usercode like '%"+usercode+"%'";
			else
				where = " usercode like '%"+usercode+"%'";
		}
		if(StringUtils.isNotEmpty(username))
		{
			if(StringUtils.isNotEmpty(where))
				where += " and username like '%"+username+"%'";
			else
				where = " username like '%"+username+"%'";
		}
		if(where!="")
			msql = msql +" where "+ where;
		System.out.println("msql="+msql);
		String countSql = "select count(*) " + msql;
		return getPageBean(msql, countSql, pageBean);
	}
	
	@Override
	public List<SysUserinfo> queryAllUserInfoList(int upperid,String usercode,String username) {
		// TODO Auto-generated method stub
		String msql = "from SysUserinfo";
		String where = "";
		if(upperid!=0)
			where += " deptid =" + upperid;
		if(usercode!="")
		{
			if(where=="")
				where = " usercode = '"+usercode+"'";
			else
				where += " and usercode = '"+usercode+"'";
		}
		if(username!="")
		{
			if(where=="")
				where = " username = '"+username+"'";
			else
				where += " and username = '"+username+"'";
		}
		if(where!="")
			msql = msql +" where "+where;
		logger.debug(msql);
		return queryBean(msql);
	}

	@Override
	public PageBean queryUserInfoByDept(int deptid) {
		// TODO Auto-generated method stub
		String msql = "from SysUserinfo where deptid="+deptid;
		String countSql = "select count(*) " + msql;
		return new PageBean(msql, countSql);
	}

	@Override
	public List<SysUserinfo> queryUserInfoListByDept(int deptid) {
		// TODO Auto-generated method stub
		String msql = "from SysUserinfo where deptid="+deptid;
		return queryBean(msql);
	}

	@Override
	public SysUserinfo getUserInfoByUserCode(String userCode) {
		// TODO Auto-generated method stub
		String msql = "from SysUserinfo where usercode='"+userCode+"'";
		logger.error("msql:"+msql);
		int nCount = queryBean(msql).size();
		if(nCount>0)
			return (SysUserinfo) queryBean(msql).get(0);
		else
			return null;
	}

	@Override
	public SysUserinfo getUserInfoByUserName(String userName) {
		// TODO Auto-generated method stub
		String msql = "from SysUserinfo where username='"+userName+"'";
		return (SysUserinfo) queryBean(msql).get(0);
	}

	@Override
	public boolean saveUserInfo(SysUserinfo userInfo) {
		// TODO Auto-generated method stub
		try {
			getHibernateTemplate().save(userInfo);
			return true;
		} catch (Exception e) {
			logger.error("新增通知异常！", e);
			return false;
		}
	}

	@Override
	public boolean updateUserInfo(SysUserinfo userInfo) {
		// TODO Auto-generated method stub
		try {
			getHibernateTemplate().update(userInfo);
			return true;
		} catch (Exception e) {
			logger.error("更新通知异常！", e);
			return false;
		}
	}

	@Override
	public boolean deleteUserInfo(SysUserinfo userInfo) {
		// TODO Auto-generated method stub
		logger.error("com.alibaba.dubbo.demo.provider.dao.impl  deleteUserInfo！");
		try {
			 getHibernateTemplate().delete(userInfo);
			 return true;
		} catch (Exception e) {
			logger.error("删除通知异常！", e);
			return false;
		}
	}

	@Override
	public PageBean queryAllUserDeptInfo(PageBean pageBean, int upperid, String usercode, String username) {
		// TODO Auto-generated method stub
		String sSql = "SELECT a.deptid,a.userid,a.username,a.usercode,a.userpwd,a.phone,a.remark,b.deptname from sys_userinfo a LEFT JOIN sys_deptinfo b on a.deptid = b.deptid";
		String sWhere = "";
		if(upperid!=0)
			sWhere += " a.deptid =" + upperid;
		if(StringUtils.isNotEmpty(usercode))
		{
			if(StringUtils.isNotEmpty(sWhere))
				sWhere = " a.usercode = '"+usercode+"'";
			else
				sWhere += " and a.usercode = '"+usercode+"'";
		}
		
		if(StringUtils.isNotEmpty(username))
		{
			if(StringUtils.isNotEmpty(sWhere))
				sWhere = " a.username = '"+username+"'";
			else
				sWhere += " and a.username = '"+username+"'";
		}
		if(sWhere!="")
			sSql = sSql +" where "+ sWhere;
		
		logger.debug("sSql="+sSql);
		return getPageBeanBySql(sSql,  pageBean);
	}

	@Override
	public List<SysUserinfo> queryAllUserDeptInfoList(int upperid, String usercode, String username) {
		// TODO Auto-generated method stub
		String sSql = "SELECT a.deptid,a.userid,a.username,a.usercode,a.userpwd,a.phone,a.remark,b.deptname from sys_userinfo a LEFT JOIN sys_deptinfo b on a.deptid = b.deptid";
		String sWhere = "";
		if(upperid!=0)
			sWhere += " a.deptid =" + upperid;
		if(StringUtils.isNotEmpty(usercode))
		{
			if(StringUtils.isNotEmpty(sWhere))
				sWhere = " a.usercode = '"+usercode+"'";
			else
				sWhere += " and a.usercode = '"+usercode+"'";
		}
		if(StringUtils.isNotEmpty(username))
		{
			if(StringUtils.isNotEmpty(sWhere))
				sWhere = " a.username = '"+username+"'";
			else
				sWhere += " and a.username = '"+username+"'";
		}
		if(sWhere!="")
			sSql = sSql +" where "+ sWhere;
		logger.debug("sSql="+sSql);
		return queryBeanBySql(sSql);
	}

	@Override
	public PageBean queryAllUserInfo(PageBean pageBean, String usercode, String username, String deptlist) {
		String msql = "from SysUserinfo";
		String where = "";
		if(StringUtils.isNotEmpty(usercode))
		{
			if(StringUtils.isNotEmpty(where))
				where += " and usercode like '%"+usercode+"%'";
			else
				where = " usercode like '%"+usercode+"%'";
		}
		if(StringUtils.isNotEmpty(username))
		{
			if(StringUtils.isNotEmpty(where))
				where += " and username like '%"+username+"%'";
			else
				where = " username like '%"+username+"%'";
		}
		if(StringUtils.isNotEmpty(deptlist))
		{
			if(StringUtils.isNotEmpty(where))
				where += " and deptid in ("+deptlist+")";
			else
				where = " deptid in ("+deptlist+")";
		}
		
		if(where!="")
			msql = msql +" where "+ where;
		System.out.println("msql="+msql);
		String countSql = "select count(*) " + msql;
		return getPageBean(msql, countSql, pageBean);
	}
}
